package h0815;

import java.util.*;

/**
  * @description 考古学家
  * @author 不知名帅哥
  * @date 2024/8/15 9:16
  * @version 1.0
*/
public class Archaeologist {
    static int n;
    static String[] arr;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = Integer.parseInt(sc.nextLine());
        arr = sc.nextLine().split(" ");

        getResult();
    }

    public static void getResult() {
        // 排序是为了让相同元素相邻，方便后面树层去重
        Arrays.sort(arr);

        boolean[] used = new boolean[n];
        LinkedList<String> path = new LinkedList<>();
        HashSet<String> res = new HashSet<>();

        dfs(used, path, res);

        // 输出石碑文字的组合（按照升序排列）
        res.stream().sorted(String::compareTo).forEach(System.out::println);
    }

    //全排列
    public static void dfs(boolean[] used, LinkedList<String> path, HashSet<String> res) {
        if (path.size()==n){ //递归条件
            StringBuilder sb = new StringBuilder();
            for (String s : path) {
                sb.append(s);
            }
            res.add(sb.toString());
            return;
        }

        for (int i = 0; i < n; i++) {
            if (used[i]) continue;

            //TODO 树层去重
            if (i>0&&arr[i].equals(arr[i-1])&&!used[i-1]) continue;
            path.addLast(arr[i]);
            used[i]=true;
            //dfs(i+1 ...)
            dfs(used,path,res);
            //进行回溯 ，used[i]=false
            used[i]=false;
            path.removeLast();
        }
    }
}
